package cn.zlj.pethome.pet.controller;

import cn.zlj.pethome.basic.QueryResult;
import cn.zlj.pethome.basic.domain.Logininfo;
import cn.zlj.pethome.basic.dto.AcceptDto;
import cn.zlj.pethome.basic.util.AjaxResult;
import cn.zlj.pethome.basic.util.LoginContext;
import cn.zlj.pethome.org.domain.TEmployee;
import cn.zlj.pethome.org.service.EmployeeService;
import cn.zlj.pethome.pet.service.ISearchMasterMsgService;
import cn.zlj.pethome.pet.domain.SearchMasterMsg;
import cn.zlj.pethome.pet.query.SearchMasterMsgQuery;
import cn.zlj.pethome.user.domain.User;
import cn.zlj.pethome.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private EmployeeService employeeService;


    /**
     * 保存和修改公用的
     * @param searchMasterMsg  传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg){
        try {
            if( searchMasterMsg.getId()!=null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.add(searchMasterMsg);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.delete(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public SearchMasterMsg get(@PathVariable("id")Long id)
    {
        return searchMasterMsgService.findById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<SearchMasterMsg> list(){

        return searchMasterMsgService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
        public QueryResult<SearchMasterMsg> json(@RequestBody SearchMasterMsgQuery query)
    {
        return searchMasterMsgService.queryByPage(query);
    }
    //publish
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request)
    {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        User user=userMapper.findByLogininfo(logininfo.getId());
        searchMasterMsg.setUserId(user.getId());
        return searchMasterMsgService.publish(searchMasterMsg);
    }
    //state 0待发布  1 审核通过 2已接单 3已完成
    @PostMapping("/toHandle")
    public QueryResult<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request)
    {
        //待处理state为1当前登录人id
        Logininfo logininfo = LoginContext.getLogininfo(request);
        TEmployee tEmployee = employeeService.selectByLogininfoId(logininfo.getId());
        query.setShopId(tEmployee.getShopId());
        query.setState(1);
        return searchMasterMsgService.queryByMsgPage(query);
    }
    @PostMapping("/toAudit")
    public QueryResult<SearchMasterMsg> toAudit(@RequestBody SearchMasterMsgQuery query)
    {
        //待审核state为0
        query.setState(0);
        return searchMasterMsgService.queryByMsgPage(query);
    }
    /** 获取当前员工 和 寻主消息id
     * reject拒单
     * @param msgId
     * @return
     */
    @PostMapping("/reject/{msgId}")
    public AjaxResult reject(@RequestBody @PathVariable("msgId")Long msgId,HttpServletRequest request)
    {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        TEmployee tEmployee = employeeService.selectByLogininfoId(logininfo.getId());
        searchMasterMsgService.reject(msgId,tEmployee);

        return  AjaxResult.me().success();
    }
    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody AcceptDto acceptDto)
    {
        System.out.println(acceptDto);
        searchMasterMsgService.accept(acceptDto);
        return  AjaxResult.me().success();
    }
}
